<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

// Edit postcode.js path from localhost to pzzazz.com


class Person extends Controller 
{
	function setVars()
	{
		$this->tpl['title'] = "Personal info";
		$this->tpl['head'] = '';
        $this->tpl['body'] = '';
        $this->tpl['sidebar'] = '<h4 class="SideBar">As Work Phone Number you may add an extention.<br />Format: (800) 555-1212 x345</h4>';
        $this->tpl['sidebar'] .= $this->tm->randomBlock();
        $this->dbtable = 'contact_people';
        $this->uid = $this->session->userdata('uid');
        $this->pid = $this->tm->get_People_id();
        $this->honorific = array(
                            '1' => 'Mrs',
                            '2' => 'Miss',
                            '3' => 'Mr',
                            '4' => 'Ms'
                            );

	}
	
	function __construct()
	{
		parent::Controller();
        //$this->load->library('profiler');
        $this->auth->CheckTaMeR(100,2);
        $this->load->model('Tamer_Model', 'tm');
        $this->load->helper('form');
		$this->setVars();
        log_message('debug', "Control Panel->person: Controler Class Initialized");
	}

	function index() 
	{
        $this->getDB();
		if(isset($_POST['save']))
		{
			$this->postCheck();
		}
		else
		{
           $this->Form();
		}
	}

    function Form()
    {
        //$tpl = (array) $this->dbdata;
        //$this->tpl = array_merge($this->tpl, $tpl);

        $this->tpl['tpljs'][]  = 'jquery';
        $this->tpl['tpljs'][]  = 'jquery.bgiframe.min';
        $this->tpl['tpljs'][]  = 'jquery.dimensions';
        $this->tpl['tplcss'][]  = 'jquery.autocomplete';
        $this->tpl['tpljs'][]  = 'jquery.autocomplete';
        $this->tpl['tpljs'][]  = 'postcode';
        $this->load->library('pquery');

        $data = (array) $this->dbdata;
        extract($data);

        $attributes = array('name' => 'person');
        $form[1]['Honorific'] = form_dropdown('Honorific', $this->honorific, $Honorific);

        $class='';
        if(isset($this->validation->First_Name_error)){
            $class='FormErr'; 
        }
        $form[1]['First Name'] = form_input(array(
                                      'name'        => 'First_Name',
                                      'id'          => 'First_Name',
                                      'value'       =>  $First_Name,
                                      'maxlength'   => '30',
                                      'size'        => '10',
                                      'class'       => $class
                                      )
                                 );
                                 
        $class='';if(isset($this->validation->Middle_Name_error)){
            $class='FormErr'; 
        }
        $form[1]['Middle Name'] = form_input(array(
                                      'name'        => 'Middle_Name',
                                      'id'          => 'Middle_Name',
                                      'value'       =>  $Middle_Name,
                                      'maxlength'   => '30',
                                      'size'        => '10',
                                      'class'       => $class
                                      )
                                 );
                                 
        $class='';if(isset($this->validation->Last_Name_error)){
            $class='FormErr'; 
        }
        $form[1]['Last Name'] = form_input(array(
                                      'name'        => 'Last_Name',
                                      'id'          => 'Last_Name',
                                      'value'       =>  $Last_Name,
                                      'maxlength'   => '30',
                                      'size'        => '10',
                                      'class'       => $class
                                      )
                                 );
                                 
        $class='';if(isset($this->validation->Street_error)){
            $class='FormErr'; 
        }
        $form['Street'] = form_input(array(
                                      'name'        => 'Street',
                                      'id'          => 'Street',
                                      'value'       =>  $Street,
                                      'maxlength'   => '50',
                                      'size'        => '30',
                                      'class'       => $class
                                      )
                                 );
        $code = '';
        if($Postcode > 0){
            $query = $this->db->getwhere('places_postcode', array('id' => $Postcode), 1);
            if ($query->num_rows() > 0) {
                $v = $query->row();
            $code = "$v->code $v->city $v->state" ;
            $this->tpl['body'] .= '<script type="text/javascript">'.$this->pquery->hide('#Postcode').'</script>';
            }else{
                $code = 'Click here and select location with mouse';
            }
        }
        
        $class='';if(isset($this->validation->Postcode_error)){
            $class='FormErr'; 
        }
        $form['Postcode or City'] =  form_input(array(
                    'name'        => 'Postcode',
                    'id'          => 'Postcode',
                    'value'       => $Postcode,
                    'size'        => '10',
                    'class'       => $class,
                    'onclick'     => 'clearValue(this)'
                    )
                    ).'<span id="postresult"><a href="#" onclick= "editzip(this)">'.$code.'</a></span>';

        $class='';if(isset($this->validation->Home_Phone_error)){
            $class='FormErr'; 
        }
        $form[2]['Home Phone'] = form_input(array(
                                      'name'        => 'Home_Phone',
                                      'id'          => 'Home_Phone',
                                      'value'       =>  $Home_Phone,
                                      'maxlength'   => '15',
                                      'size'        => '12',
                                      'class'       => $class
                                      )
                                 );

        $class='';if(isset($this->validation->Cell_Phone_error)){
            $class='FormErr'; 
        }
        $form[2]['Cell Phone'] = form_input(array(
                                      'name'        => 'Cell_Phone',
                                      'id'          => 'Cell_Phone',
                                      'value'       =>  $Cell_Phone,
                                      'maxlength'   => '15',
                                      'size'        => '12',
                                      'class'       => $class
                                      )
                                 );

        $class='';if(isset($this->validation->Work_Phone_error)){
            $class='FormErr'; 
        }
        $form[2]['Work Phone'] = form_input(array(
                                        'name'        => 'Work_Phone',
                                        'id'          => 'Work_Phone',
                                        'value'       =>  $Work_Phone,
                                        'maxlength'   => '23',
                                        'size'        => '20',
                                        'class'       => $class
                                        )
                                    );

        $class='';if(isset($this->validation->Date_of_Birth_error)){
            $class='FormErr'; 
        }
        $form['Date of Birth'] = form_input(array(
                                      'name'        => 'Date_of_Birth',
                                      'id'          => 'Date_of_Birth',
                                      'value'       =>  $Date_of_Birth,
                                      'maxlength'   => '30',
                                      'size'        => '10',
                                      'class'       => $class
                                      )
                                 );


        $class='';if(isset($this->validation->Website_error)){
            $class='FormErr'; 
        }
        $form['Personal Website'] = form_input(array(
                                      'name'        => 'Website',
                                      'id'          => 'Website',
                                      'value'       =>  $Website,
                                      'maxlength'   => '200',
                                      'size'        => '40',
                                      'class'       => $class
                                      )
                                 );
        $form[5][' '] =          form_submit(array('class' => 'submit', 
                                        'name'        => 'save',
                                        'value'       => 'Save',
                                        'style'       => 'margin:10px'
                                        )
                                    );
        $form[5]['  '] = '<input type="reset" value="Reset" />';


        $this->load->library('table');
        $this->tpl['body'] .=  form_open('control_panel/person/', $attributes);
        $this->tpl['body'] .= $this->table->form_table($form);
        $this->tpl['body'] .= '</form>';
        if($Postcode > 0){
            $this->tpl['body'] .= '<script type="text/javascript">'.$this->pquery->hide('#Postcode').'</script>';
        }
        $this->load->view('blank', $this->tpl);
    } 

    function getDB()
    {
        $s = 'Honorific, First_Name, Middle_Name, Last_Name, Date_of_Birth, Street, 
                Postcode, Cell_Phone, Home_Phone, Work_Phone, Website';
        $this->db->select($s);
        $query = $this->db->getwhere($this->dbtable, array('id' => $this->pid), 1);
        if($query->num_rows() > 0) {
            $this->dbdata = $query->row();
        }
    }

	function postCheck()
	{
		$this->load->library('validation');
        $rules['Honorific']     = 'trim|required|xss_clean';
        $rules['First_Name']    = 'trim|required|min_length[2]|max_length[30]|alpha_numeric|xss_clean';
        $rules['Middle_Name']   = 'trim|max_length[30]|alpha_numeric|xss_clean';
        $rules['Last_Name']     = 'trim|required|min_length[2]|max_length[30]|alpha_numeric|xss_clean';
        $rules['Postcode']	    = 'callback_postcode_check';
        $rules['Street']        = 'trim|required|min_length[5]|max_length[50]|xss_clean';
        $rules['Cell_Phone']    = 'trim|max_length[15]|xss_clean';
        $rules['Home_Phone']    = 'trim|max_length[15]|xss_clean';
        $rules['Work_Phone']    = 'trim|max_length[23]|xss_clean';
        $rules['Date_of_Birth'] = 'trim|required|min_length[5]|max_length[20]|xss_clean';
        $rules['Website']       = 'trim|xss_clean|prep_url';
		$this->validation->set_rules($rules);
		
		log_message('debug', "Register: Validation Rules set");
		if ($this->validation->run() === FALSE)
		{
            $Honorific = $this->validation->Honorific;
            $this->tpl['select_Honorific'] = form_dropdown('Honorific', $this->honorific, $Honorific);
          
            $i = (array) $this->dbdata;
            foreach($this->dbdata as $k => $v){
                if(isset($this->validation->$k)){
                    $i[$k] = $this->validation->$k;
                }
            }
            $this->dbdata = (object) $i;
			$this->Form();
		}
		else
		{
            $data = (array) $this->dbdata;
            foreach($this->dbdata as $k => $v){
                if(isset($this->validation->$k)){
                    if(strstr($k, 'Phone') && isset($this->validation->$k)){
                        $this->validation->$k = format_phone($this->validation->$k);
                    }
                    $data[$k] = $this->validation->$k;
                }else{
                    $data[$k] = '';
                }
            }
            $this->load->library('dbStringIt');
            $dbd = (array) $this->dbdata;
            $data = new_value($dbd, $data);
            if(count($data) != 0){
                $this->db->where('id', $this->pid);
                $this->db->update('contact_people', $data);
                $this->msg->setMsg('Updated');
            }else{
                $this->msg->setError('Nothing to update');
            }
            $this->getDB();
            $this->Form();
		}
	}

    function postcode_check($str)
	{
		if ($str < 10000000 || $str > 20000000)
		{
            $this->validation->set_message('postcode_check', 'You must select the %s with the mouse from the list');
			return FALSE;
		}
		else
		{
            $this->validation->Postcode = $str;
			return TRUE;
		}
	}
}
?>
